package com.toocms.jinjuhang.ui.live_streaming

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.data.index.RoomInfoItemData
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class LiveStreamingItemModel(
    viewModel: BaseViewModel<*>, val item: ObservableField<RoomInfoItemData>
) : MultiItemViewModel<BaseViewModel<*>>(viewModel) {
    val commodityItems by lazy {
        val result = ObservableArrayList<LiveStreamingCommodityItemModel>()
        item.get()?.goods?.forEach {
            result += LiveStreamingCommodityItemModel(viewModel, ObservableField(it))
        }
        result
    }
    val commodityItemBinding = ItemBinding.of<LiveStreamingCommodityItemModel>(
        BR.liveStreamingCommodityItemModel, R.layout.item_live_streaming_commodity
    )

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply {
            viewModel.showToast("roomid=${roomid}")
            if (!roomid.isNullOrEmpty()) {
                openWechat(roomid)
            }
        }
    })

    init {
        itemType = Constants.RecyclerViewItemType.ITEM_TYPE_ONE
    }

    fun statusName(statusCode:String)=when(statusCode){
        "101"->"直播中"
        "102"->"未开始"
        "103"->"回放"
        "104"->"禁播"
        "105"->"暂停"
        "106"->"异常"
        "107"->"已过期"
        else -> "异常"
    }

    /**
     * 打开微信小程序
     */
    fun openWechat(roomId: String) {
        viewModel.showToast("打开小程序，roomId=${roomId}")
        val api= WXAPIFactory.createWXAPI(
            viewModel.getApplication(),
            Constants.ThirdPartyInfo.WECHAT_APP_KEY
        )
        val req = WXLaunchMiniProgram.Req()
        req.userName = Constants.ThirdPartyInfo.WECHAT_ORIGINAL_ID
        req.path = "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}"
//        req.path = "pages/live-player-plugin?room_id=${roomId}"
//        req.miniprogramType=WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE
        api.sendReq(req)
    }
}